
'use strict';
 
module.exports = app => {
  const { STRING, INTEGER, DATE,BIGINT } = app.Sequelize;
  const Buildings = app.model.define('buildings', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    status: INTEGER,          //建筑物状态       0：已删除 1：正常 2：封锁
    building_name: STRING(8), //建筑名称
    type: INTEGER,            //建筑类型         0: 限制区域 1:电影院  2:传送点 100:电影院内部  101:住宅区
    position_x: BIGINT,       //建筑物x轴坐标
    position_y: BIGINT,       //建筑物y轴坐标
    building_size: STRING(16),//建筑物大小
    stay_num: {
      type: app.Sequelize.INTEGER, 
      validator:{
        max: 2,
      }
    },        //停留人数
    sign_up_time: DATE        //创建时间
  },{
    freezeTableName: true,    // Model 对应的表名将与model名相同
    timestamps: false,
  });
  //建筑物停留人数自增
  Buildings.findOneAndEnter = async function(obj) {
    let building = await this.findOne(obj);
    if(!building) return;
    building = await building.increment('stay_num')
    return building;
  }
  //建筑物停留人数自减
  Buildings.findOneAndLeave = async function(obj) {
    let building = await this.findOne(obj);
    building = await building.decrement('stay_num')
    return building;
  }
  return Buildings;

};